package com.andy.controller;

import com.andy.dao.entity.Article;
import com.andy.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @RequestMapping("/index")
    public String indexPage(Model model){
        Page<Article> page = articleService.getArticles(0);
        model.addAttribute("articles",page.getContent());
        model.addAttribute("totalPages",page.getTotalPages());
        model.addAttribute("pageStart",page.getPageable().getPageNumber());

        return "client/index";
    }

    @RequestMapping("/getArticles")
    public String getArticles(Model model,int pageStart){
        Page<Article> page = articleService.getArticles(pageStart);
        model.addAttribute("articles",page.getContent());
        model.addAttribute("totalPages",page.getTotalPages());
        model.addAttribute("pageStart",page.getPageable().getPageNumber());

        return "client/index::articlesDiv";
    }

}
